package club.beimeng.emailserver2.controller;

import club.beimeng.emailserver2.entity.from.SendByHtml;
import club.beimeng.emailserver2.entity.from.SendByTemplate;
import club.beimeng.emailserver2.service.EmailService;
import club.beimeng.emailserver2.utils.CheckEmailNumber;
import club.beimeng.emailserver2.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;


@RestController
@RequestMapping("/emailSever2/email")
public class EmailController { // 提供账户的相关操作
    @Autowired
    public EmailService emailService;

    @ApiOperation("自定义html")
    @PostMapping("sendByHtml")
    public R emailByText(@RequestBody SendByHtml sendByHtml){
        if (CheckEmailNumber.checkThemIllegal(sendByHtml.getRecipients()) != null){ // 如若不合法
            return R.error().data("不合法账号位置及名称",CheckEmailNumber.checkThemIllegal(sendByHtml.getRecipients()));
        }
        boolean b = emailService.receiveTask(sendByHtml.getRecipients(),sendByHtml.getTitle(),sendByHtml.getHtml());
        return b? R.ok().message("邮件将在10秒内送达"):R.error();
    }

    @ApiOperation("自定义html")
    @PostMapping("sendByTemplete")
    public R emailByText(@RequestBody SendByTemplate sendByTemplate){
        if (CheckEmailNumber.checkThemIllegal(sendByTemplate.getRecipients()) != null){ // 如若不合法
            return R.error().data("不合法账号位置及名称",CheckEmailNumber.checkThemIllegal(sendByTemplate.getRecipients()));
        }
        for (String s : sendByTemplate.getArgs().keySet()) {
            System.out.println("s = " + s);
            System.out.println("args.get(s) = " + sendByTemplate.getArgs().get(s));
        }

        boolean b = emailService.receiveTask(sendByTemplate.getRecipients(),sendByTemplate.getTitle(),sendByTemplate.getTemplateCode(),sendByTemplate.getArgs());
        return  b? R.ok().message("邮件将在10秒内送达"):R.error();
    }

    @ApiOperation("提供MimeMessage")
    @PostMapping("sendByMimeMessage")
    public R emailByMimeMessage(MimeMessage mimeMessage){
        boolean b = emailService.receiveTask(mimeMessage);
        return  b? R.ok().message("邮件将在10秒内送达"):R.error();
    }

}
